
// #ifdef H5
import cc from "../static/index1.js";
export default function(url,type='a', size = 1) {
	var _a=["a","b","c","d","e","f","g","h","i"]
	
	let all=cc;
	return new Promise((a, b) => {
		if(_a.indexOf(type)==-1){
			console.log(type)
			b('方法不存在,确保参数2正确')
			a('方法不存在,确保参数2正确')
			return false
		}
			let img = new Image();
			img.src =url;
			img.onload = function() {
				captureImage(img)
			};
		var captureImage = function(img) {
			var canvas = document.createElement("canvas");
			canvas.width = img.width;
			canvas.height = img.height;
			let ctx = canvas.getContext('2d');
			ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
			let canvasData = ctx.getImageData(0, 0, canvas.width, canvas.height);
			ctx.putImageData(all[type](canvasData), 0, 0)
			a(canvas.toDataURL("image/png"))
		};
	})
}
// #endif
//#ifdef APP-PLUS
export default function(url,type='a', size = 1) {
	let WEB, _cc;
	return new Promise((a, b) => {
		var _a=["a","b","c","d","e","f","g","h","l"]
		if(_a.indexOf(type)==-1){
			b('方法不存在,确保参数2正确')
			a('方法不存在,确保参数2正确')
			return false
		}
		
		function randomCoding(){
		 	//创建26个字母数组
		 	var arr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
		 	var idvalue ='';
		 	for(var i=0;i<10;i++){
		    	idvalue+=arr[Math.floor(Math.random()*26)];
		 	}
			return idvalue;
		 }
		let _storagename=randomCoding()
		WEB = plus.webview.create(
			'_www/uni_modules/mcc-imagefilter/static/index.html', _storagename, {
				'uni-app': 'none',
				"top":"1000px",
				"background": 'transparent',
				"width": 0,
				"height": 0,
				"webviewBGTransparent":true,
				"left":"1000px",
				"opacity":"0",
				
			}, {
				src: url,
				type:type,
				size: size,
				name:_storagename,
				js:"_www/uni_modules/mcc-imagefilter/static/index1.js"
			});
			
		WEB.show();

		_cc = setInterval(() => {
			if (plus.storage.getItem(
					_storagename)) {
				let img = plus.storage.getItem(
					_storagename);
				plus.storage.removeItem(
					_storagename);
				plus.webview.close(WEB)
				a(img)
			};
			if (!plus.webview.getWebviewById(_storagename)) {
				plus.storage.removeItem(
					_storagename);
				plus.webview.close(WEB)
				clearInterval(_cc)
			}
			
		}, 50)
	})
}
//#endif